package com.sm.service.Impl;

import com.sm.dao.AttendanceDao;
import com.sm.dao.UserDao;
import com.sm.entity.Attendance;
import com.sm.entity.AttendanceRec;
import com.sm.service.AttendanceService;
import com.sm.utils.AttendanceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    AttendanceDao attendanceDao;
    @Autowired
    UserDao userDao;

    public AttendanceRec atNow() throws Exception {
        isAtRec();
        AttendanceRec attendanceRec = new AttendanceRec();
        attendanceRec = attendanceDao.findAttendanceCount(AttendanceUtil.getDateStr());
        return attendanceRec;
    }

    public List<AttendanceRec> atRecently() throws Exception {
        return attendanceDao.findAtCounts();
    }

    public List<Attendance> searchOneByUid(Integer uid) throws Exception {
        return attendanceDao.findAttendanceByUid(uid,AttendanceUtil.getMonth());
    }

    public void signIn(Integer uid,Integer sign) throws Exception {
        isAtRec();
        Date now = new Date();
        Date nine = new Date(0,0,0,9,0,0);
        Date ten = new Date(0,0,0,10,0,0);
        Attendance attendance = new Attendance();
        attendance.setUid(uid);
        attendance.setDate(now);
        if(sign==1){
            //上班签到
            Integer status = 0;
            if(now.before(nine))
                status = 1;
            else if (now.before(ten)){
                status = 2;
            } else {
                status = 3;
            }
            attendance.setStarttime(now);
            attendance.setStatus(status);
//            attendanceDao.deleteAttendance(uid,AttendanceUtil.getDelMonth());
            attendanceDao.addAttendance(attendance);
            attendanceDao.updateAtCount(AttendanceUtil.getDateStr());
            return;
        }
        //下班签到
        attendance.setEndtime(now);
        attendance.setFlag(1);
        attendanceDao.updateAttendance(attendance);
        //计算今天工作了多久
        Integer status = attendanceDao.findAtStatus(uid,AttendanceUtil.getDateStr());
        Integer minute = attendanceDao.findWorkMinute(uid,AttendanceUtil.getDateStr());
        if (status == 2){
            //添加迟到奖励表
        }else if (status == 3){
            //添加旷工奖励表
        }
        Calendar isweekday = new GregorianCalendar();
        //这里判断当天是否周末 计算加班时间
        if(isweekday.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || isweekday.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            attendanceDao.updateATotalOverTimeB(uid,AttendanceUtil.getMonth(),minute);
            attendanceDao.updateSign(uid,AttendanceUtil.getDateStr(),2);
        }else{
            attendanceDao.updateATotalTime(uid,AttendanceUtil.getMonth(),minute);
        }
    }

    public List<Boolean> isSign(Integer uid) throws Exception {
        List<Boolean> flags = new ArrayList<Boolean>();
        Integer flagup = attendanceDao.findAtAid(uid,AttendanceUtil.getDateStr());
        if(flagup==null) {
            //上班是否签到了
            flags.add(false);
        }else {
            flags.add(true);
            Integer flagdown = attendanceDao.findAtFlag(uid,AttendanceUtil.getDateStr());
            //下班是否签到了
            if (flagdown == 1){
                flags.add(true);
            }else flags.add(false);
        }
        //是否加班了
        Integer sign = attendanceDao.findAtSign(uid,AttendanceUtil.getDateStr());
        if(sign == null||sign == 0){
            flags.add(false);
        }else flags.add(true);
        return flags;
    }

    public void addOverTime(Integer uid, Integer sign) throws Exception {
        Attendance attendance = new Attendance();
        Date ostime = new Date(0,0,0,19,0,0);
        Date oetimea = new Date(0,0,0,20,0,0);
        Date oetimeb = new Date(0,0,0,21,0,0);
        attendance.setUid(uid);
        attendance.setOstime(ostime);
        attendance.setDate(new Date());
        Calendar isweekday = new GregorianCalendar();
        if(isweekday.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || isweekday.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            attendance.setSign(2);
            if(sign == 1){
                attendance.setOetime(oetimea);
                attendanceDao.updateOverTime(attendance);
                attendanceDao.updateATotalOverTimeB(uid,AttendanceUtil.getMonth(),60);
            }else if(sign == 2){
                attendance.setOetime(oetimeb);
                attendanceDao.updateOverTime(attendance);
                attendanceDao.updateATotalOverTimeB(uid,AttendanceUtil.getMonth(),120);
            }
        }else{
            attendance.setSign(1);
            if(sign == 1){
                attendance.setOetime(oetimea);
                attendanceDao.updateOverTime(attendance);
                attendanceDao.updateATotalOverTimeA(uid,AttendanceUtil.getMonth(),60);
            }else if(sign == 2){
                attendance.setOetime(oetimeb);
                attendanceDao.updateOverTime(attendance);
                attendanceDao.updateATotalOverTimeA(uid,AttendanceUtil.getMonth(),120);
            }
        }
        //这里缺少一个判断 周末加班2倍工资 现在是周末加班按1.5倍计算了

    }

    public List<Attendance> atTable(Integer uid,String username ,String name) throws Exception {
        List<Attendance> list = new ArrayList<Attendance>();
        if(uid != null){
            list = attendanceDao.findAttendanceByUid(uid, AttendanceUtil.getMonth());
        }else {
            List<Integer> uids = userDao.findUidByUsernameOrName(username,name);
            for(Integer auid:uids){
                list.addAll(attendanceDao.findAttendanceByUid(auid, AttendanceUtil.getMonth()));
            }
        }
        return list;
    }


    /**
     * 判断当天是否创建了考勤比例表 没有的话 创建
     */
    public void isAtRec() throws Exception {
        String dateStr = AttendanceUtil.getDateStr();
        Integer recId = attendanceDao.findAtRecId(dateStr);
        if (recId == null ){
            attendanceDao.addAttendanceRec(dateStr);
        }
    }

}
